package ssau.knyazev.db;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class QueryParser {

	public static String[] parseSelectQuery(String src) {
		String[] res = null;
		try {
			String[] arr = src.split("where");
			if (arr.length == 1)
				return null;
			String temp = arr[1];
			arr = temp.split(" and | or ");
			res = new String[arr.length];
			for (int i = 0; i < arr.length; i++) {
				res[i] = arr[i].split("=")[0].trim();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return res;
	}
	
	public static Map<String, String> parseSelectQueryForColumns(String src, boolean inv) {
		Map<String, String> res = null;
		try{
			String[] arr = src.split("select | from ");
	        src = arr[1];
	        if (src.trim().equals("*")){
	        	return null;
	        }
	        arr = src.split(", ");
		    res = new HashMap<String, String>();
	        for (int i = 0; i < arr.length; i++){
	        	String[] arr2 = arr[i].split(" as ");
	        	String beforeAs = arr2[0].trim().toUpperCase();
	        	String afterAs = beforeAs;
	        	if (arr2.length > 1 && !arr2[1].trim().equals("")){
	        		afterAs = arr2[1].trim().toUpperCase().replaceAll("\"", "");
	        	}
	        	
	        	if (beforeAs.contains("TO_CHAR")){
	        		beforeAs = beforeAs.replaceAll("TO_CHAR\\(", "");
	        		beforeAs = beforeAs.replaceAll(",\'FM99990D990\'\\)", "");
	        	}
	        	if (inv){
	        		res.put(beforeAs, afterAs);
	        	} else {
	        		res.put(afterAs, beforeAs);
	        	}	        	
	        }
		}catch(Exception e){
			e.printStackTrace();
		}
        
		return res;
	}

	public static List<String> parseInsertQuery(String src) {
		List<String> res = new LinkedList<String>();
		try {
			String temp = null;
			String[] arr = src.split(" into | values");
			temp = arr[1];
			arr = temp.split("\\(|\\)");
			temp = arr[1];
			arr = temp.split(",");
			for (int i = 0; i < arr.length; i++) {
				res.add(arr[i].trim().toUpperCase());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public static List<String> parseDeleteQuery(String src){
		List<String> res = null;
		try {
			String[] arr = src.split("where");
			if (arr.length == 1)
				return null;
			String temp = arr[1];
			arr = temp.split(" and | or ");
			res = new LinkedList<String>();
			for (int i = 0; i < arr.length; i++) {
				res.add(arr[i].split("=")[0].trim().toUpperCase());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public static List<String> parseUpdateQuery(String src){
		List<String> res = new LinkedList<String>();
		String[] arr = src.split(" where | set ");
		String temp = arr[1];
		String[] tarr = temp.split(",|\\?|=");
		for(String x: tarr){
			if (!x.trim().equals("")){
				res.add(x.trim().toUpperCase());
			}
		}
		tarr = arr[2].split(" and | or ");
		for(String x: tarr){
			String[] s = x.split("=");
			if (!s[0].trim().equals("")){
				res.add(s[0].trim().toUpperCase());
			}
		}
		return res;
	}

	public static void printTokens(List<String> src) {
		try {
			System.out.println("lenght = " + src.size());
			for (String x : src)
				System.out.println("token: " + x);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
